//
//  AuthenticationController.swift
//  Fancyswap
//
//  Created by Karim Safraoui on 2015-03-14.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import UIKit

class AuthenticationViewController : UITableViewController {
    
    @IBOutlet weak var login: UITextField!
    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var authentication_btn: UIButton!
    @IBOutlet weak var remeberme: UISwitch!
    
    // for move up view when keyboard appear
    @IBOutlet var textFieldToBottomLayoutGuideConstraint: NSLayoutConstraint!

    
    let t = FancyTools()
    var dismissDelegate: protocol<FancyViewControllerProtocol>?

    override func viewDidLoad() {
        
        super.viewDidLoad()
        var tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "closeKeyboard")
        self.view.addGestureRecognizer(tap)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name:UIKeyboardWillShowNotification, object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name:UIKeyboardWillHideNotification, object: nil);
    }
    
    func keyboardWillShow(sender: NSNotification) {
        if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            self.textFieldToBottomLayoutGuideConstraint?.constant += keyboardSize.height
        }
    }
    
    func keyboardWillHide(sender: NSNotification) {
        if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            self.textFieldToBottomLayoutGuideConstraint?.constant -= keyboardSize.height
        }
    }

    func closeKeyboard() {
        self.view.endEditing(true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func authentiction(sender: UIButton) {
        
        let auth = Authentification()
    
            let login_var   = login.text
            let pwd_var     = password.text
            let remb        = remeberme.on
    
        
            if(login_var.isEmpty || pwd_var.isEmpty){
                
                t.alert_push(  "Attention",msg:"Veuillez saisir votre login et pwd")
            }
            else{
            
                t.web_service(  {(json_response : JSON) -> () in
                                
                                    let auth = Authentification()
                                    if( auth.authenticate( json_response, remember:remb, un:login_var, pw:pwd_var  ) == true ){
                                        self.dismiss(sender)
                                    }
                                },
                                type_protocol:"POST",
                                rest_fancy_end_point: "login",
                                params: ["email":login_var,"password":pwd_var],
                                encodeJson:true
                            );
            }
    }
    
    @IBAction func home(sender: AnyObject) {
        var storyboard = UIStoryboard(name: "Main", bundle: nil)
        var controller = storyboard.instantiateViewControllerWithIdentifier("main") as UIViewController
        self.presentViewController(controller, animated: true, completion: nil)
    }
    
    func dismiss(sender: AnyObject) {
        
        if self.dismissDelegate != nil {
            self.dismissDelegate!.dismissViewControllerAndTakeAction(self)
            return
        }
        
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}